package com.xes.attend.server;


import com.xes.attend.config.ServerConfig;
import com.xes.attend.domain.AttendData;
import com.xes.attend.domain.BaseException;
import com.xes.attend.domain.Result;
import com.xes.attend.domain.ResultCode;
import com.xes.attend.repo.AttendDataDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/admin")
public class AdminController {

	private static final Logger log = LogManager.getLogger(AdminController.class);

	@Autowired
	private ServerConfig config;
	@Autowired
	private AttendDataDao attendDataDao;

	@RequestMapping("/view")
	public String view() {
		log.info("AdminController is view {}", "info");
		return "system is very healthy";
	}
	
	@RequestMapping("/config")
	public Result<ServerConfig> Config() {
		Result<ServerConfig> result = new Result<>();
		result.setData(config);
		result.setSuccess(true);
		result.setCode(ResultCode.SUCCESS.getCode());
		result.setMessage(ResultCode.SUCCESS.getMessage());
		return result;
	}

	@RequestMapping("/querydata")
	public Result<Iterable<AttendData>> queryAttendData() throws BaseException {
		Iterable<AttendData> dataList = attendDataDao.findAll();

		Result<Iterable<AttendData>> result = new Result<>();
		if (null != dataList) {
			result.setSuccess(true);
			result.setCode(ResultCode.SUCCESS.getCode());
			result.setMessage(ResultCode.SUCCESS.getMessage());
			result.setData(dataList);
		} else {
			result.setSuccess(false);
			result.setCode(ResultCode.ERROR.getCode());
			result.setMessage(ResultCode.ERROR.getMessage());
			result.setData(dataList);
		}
		return result;
	}

	@RequestMapping("/querycount")
	public Result<Map<String, Long>> queryAttendCount() throws BaseException {
		Result<Map<String, Long>> result = new Result<>();
		Map<String, Long> attendDataCount = new HashMap<String, Long>();

		long total = attendDataDao.countByIsDelete(0);
		long noattend = attendDataDao.countByIsAbsentAndIsDelete(1, 0);
		long isattend = attendDataDao.countByIsAbsentAndIsDelete(0, 0);

		attendDataCount.put("total", total);
		attendDataCount.put("noattend", noattend);
		attendDataCount.put("isattend", isattend);

		result.setSuccess(true);
		result.setCode(ResultCode.SUCCESS.getCode());
		result.setMessage(ResultCode.SUCCESS.getMessage());
		result.setData(attendDataCount);

		return result;
	}

	@RequestMapping("/cleardata")
	public Result<String> clearAttendData() throws BaseException {
		Result<String> result = new Result<>();

		// 清除所有考勤数据
		attendDataDao.deleteAll();
		// 重置预考勤数据下载状态
		config.setDownloadAttendByToday(false);

		result.setSuccess(true);
		result.setCode(ResultCode.SUCCESS.getCode());
		result.setMessage(ResultCode.SUCCESS.getMessage());
		return result;
	}
}
